package com.heima.wemedia.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.wemedia.service.WemediaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/wemedia")
public class WemediaController {

    @Autowired
    private WemediaService wemediaService ;

    /**
     * 增加自媒体人
     * @param wmUser
     * @return
     */
    @PostMapping("/save")
    public ResponseResult wemediaSave(@RequestBody WmUser wmUser){

        boolean result = wemediaService.save(wmUser);

         return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    /**
     *
     * 根据自媒体人姓名查询自媒体人信息
     * @param name
     * @return
     */
    @GetMapping("/find/{name}")
    public ResponseResult wemediafind(@PathVariable("name") String name){
        LambdaQueryWrapper<WmUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WmUser::getName,name);
        List<WmUser> list = wemediaService.list(lambdaQueryWrapper);
        if(list != null && !list.isEmpty()){
            //11
            return ResponseResult.okResult(list.get(0));
        }

        /*for (WmUser wmUser : list) {
            return ResponseResult.okResult(wmUser);
        }*/

        return ResponseResult.errorResult(AppHttpCodeEnum.err_code) ;
    }

}
